package com.qunar.train.mock.config;

import com.qunar.train.conf.cfg.Config;
import com.qunar.train.conf.model.Parameter;
import com.qunar.train.conf.properties.ConfPropChangeListener;
import com.qunar.train.conf.utils.ConfClientUtils;
import com.qunar.train.mock.util.ConfigHandle;

import java.util.LinkedHashMap;
import java.util.Map;

import mockit.Mock;
import mockit.MockUp;

public class MockTrainConfig {
    public static final String TRAIN_CONFIG_PATH = "trainConfig/";
    public static final String TRAIN_CONFIG_NAME = "train_conf.properties";
    public static MockTrainConfig instance = new MockTrainConfig();

    public static void setProperty(String key, String value) {
        String filepath = TRAIN_CONFIG_PATH + TRAIN_CONFIG_NAME;
        ConfigHandle.configMapSet(filepath, key, value);
    }

    public static String getProperty(String key) {
        String filepath = TRAIN_CONFIG_PATH + TRAIN_CONFIG_NAME;
        Map<String, String> map = ConfigHandle.loadConfig(filepath);
        String value = map.get(key);
        return value;
    }

    public static void mock() {
        new ConfClientUtilsMockUp();
    }

    public static class ConfClientUtilsMockUp extends MockUp<ConfClientUtils> {
        @Mock
        public void $clinit() {
        }

        @Mock
        public void $init() {
        }

        @Mock
        public static String getProperty(String key) {
            return MockTrainConfig.getProperty(key);
        }

        @Mock
        public static String getProperty(String key, String defaultValue) {
            Map<String, String> map = ConfigHandle.loadConfig(TRAIN_CONFIG_PATH + TRAIN_CONFIG_NAME);
            if (map == null || map.get(key) == null) {
                return defaultValue;
            }
            return map.get(key);
        }

        @Mock
        public static long getLongProperty(String key, long defaultValue) {
            String temp = MockTrainConfig.getProperty(key);
            if (temp != null) {
                return Long.valueOf(temp);
            } else {
                return defaultValue;
            }
        }

        @Mock
        public static void setConfPropChangeListener(ConfPropChangeListener e) {
        }

        /**
         * 使用请谨慎
         */
        @Mock
        public static void addData(String key, String value, String desc) {
            MockTrainConfig.setProperty(key, value);
        }

        /**
         * 使用请谨慎
         */
        @Mock
        public static void setData(String key, String value, String desc) {
            MockTrainConfig.setProperty(key, value);
        }

        /**
         * 使用请谨慎
         */
        @Mock
        public static void addData(Config cfg, String key, String value, String desc) {
            MockTrainConfig.setProperty(key, value);
        }

        @Mock
        public static Map<String, Parameter> getData(String path) {
            String filepath = TRAIN_CONFIG_PATH + TRAIN_CONFIG_NAME;
            Map<String, String> map = ConfigHandle.loadConfig(filepath);
            Parameter parameter = new Parameter();
            Map<String, Parameter> mapsp = new LinkedHashMap<String, Parameter>();
            for (String key : map.keySet()) {
                parameter.setName(key);
                parameter.setValue(map.get(key));
                mapsp.put(key, parameter);
            }
            return mapsp;
        }

    }

}